<?php
class Table
{
	public $table_name = null;
	public $pk_name = 'id';
	public $pk_value = null;
	private $column_values = array();

	public function __get($k=null)
	{
		if ( isset($this->column_values[$k]) )
			return $this->column_values[$k];
		return null;
	}

	public function __set($k=null, $v=null)
	{
		$this->values[$k] = $v;
	}

	public function _set_values($vs=array())
	{
		$this->column_values = $vs;
	}

	public function __construct($n=null, $record=array())
	{
		$this->table_name = $n;
		$this->_set_values( $record );
	}

	public function _set_pk($k=null, $v=null)
	{
		if ( $k && $v )
		{
			$this->pk_name = $k;
			$this->pk_value = $v;
		}
	}

	public function get($k=null)
	{
		if (null==$k)
			return $this->column_values;
		return $this->__get($k);
	}

	public function set($k, $v=null)
	{
		$this->column_values[$k] = $v;
	}

	public function plus($k=null, $v=1)
	{
		if ( array_key_exists($k, $this->column_values) )
		{
			$this->column_values[$k] += $v;
		}
		else throw new Exception( 'Table ' .$this->table_name. ' no column '. $k );
	}

	public function update()
	{
		$fields = func_get_args();
		if ( empty($fields) )
			return true;
		
		$up_array = array();
		foreach( $fields AS $f )
		{
			if ( array_key_exists($f, $this->column_values) )
			{
				$up_array[$f] = $this->column_values[$f];

			}
		}
		if (empty($up_array) )
			return true;

		return DB::Update($this->table_name, $this->pk_value, $up_array, $this->pk_name);
	}

	static public function Fetch($n=null,$ids=array(),$k='id')
	{
		if ( empty($ids) ) 
			return array();

		$single = is_array($ids) ? false : true;

		settype($ids, 'array');
		$ids_str = join(',', $ids);
		$sql = "SELECT * FROM $n WHERE `$k` IN ($ids_str)";
		$rows = DB::GetQueryResult( $sql, false );

		$i_rows = array();
		foreach( $rows AS $one )
		{
			$i_rows[$one[$k]] = $one;
		}
		
		$s_rows = array();
		foreach( $ids AS $id)
		{
			if ( isset($i_rows[$id]) )
			{
				$s_rows[$id] = $i_rows[$id];
			}
		}

		return $single ? array_shift($s_rows) : $s_rows;
	}

	static public function Load($n=null, $id=1, $k='id')
	{
		$row = self::Fetch($n,$id,$k);
		
		if ( $row )
		{
			$table = new Table($n);
			$table->_set_pk( $k, $id );
			$table->_set_values($row);
			return $table;
		}
		return null;
	}

	static public function Loads($n=null, $ids=array(), $k='id')
	{
		$rows = self::Fetch($n,$ids,$k);

		$tables = array();
		foreach ( $rows AS $one )
		{
			$table = new Table($n);
			$table->_set_pk( $k, $one[$k] );
			$table->_set_values($one);

			$tables[$one[$k]] = $table;
		}

		return $tables;
	}

	static public function LimitQuery($n=null, $options=array(), $k='id')
	{
		$k = isset($options['k']) ? $options['k'] : 'id';

		$rows = DB::LimitQuery( $n, $options );

		$tables = array();
		foreach ( $rows AS $one )
		{
			$table = new Table($n);
			$table->_set_pk( $k, $one[$k] );
			$table->_set_values($one);

			$tables[$one[$k]] = $table;
		}

		return $tables;
	}

	static public function Count($n=null, $condition=null)
	{
		$condition = DB::BuildCondition( $condition );
		$condition = null==$condition ? null : "WHERE $condition";
		$sql = "SELECT COUNT(1) AS count FROM $n $condition";
		$row = DB::GetQueryResult($sql, true);
		return intval($row['count']);
	}

	static public function Delete($n=null, $id=null, $k='id')
	{
		settype( $id, 'array' );
		$id = join(',', $id);
		$sql = "DELETE FROM $n WHERE `id` IN($id)";
		return DB::Query( $sql );
	}
}
?>
